var pageNow=1;
var pageMax;
function dele(id) {
    if (confirm("是否删除这条新闻？")) {
        $.ajax({
            url:"easybuy/deleNews",
            data:{
                id:id
            },
            success:function (data) {
                if(data=="true"){
                    alert("删除成功！");
                    location.reload();
                }else {
                    alert("删除失败！")
                }
            }
        })
    }

}
function showList(newsList) {
    $(".m_right table").empty();
    var $table=$(".m_right table");
    var user=JSON.parse(sessionStorage.getItem("user"));
    if (user!=null&&user.type == 1) {
        $($table).append("<tr>\n" +
            "                    <th>新闻编号</th>\n" +
            "                    <th>新闻标题</th>\n" +
            "                    <th>创建时间</th>\n" +
            "                    <th colspan=\"2\">操作</th>\n" +
            "                </tr>")
        for(var i in newsList){
            var $tr=$("<tr>" +
                "<td>"+newsList[i].id+"</td>" +
                "<td><a href='Member_News_Show.html?id="+newsList[i].id+"'>"+newsList[i].title+"</a></td>" +
                "<td>"+newsList[i].createTime+"</td>" +
                "<td><a href='Member_News_Modify.html?id="+newsList[i].id+"'>修改</a></td>" +
                "<td><a href='javascript:void(0)' onclick='dele("+newsList[i].id+")'>删除</a></td>" +
                "</tr>")
            $($table).append($tr);
        }
    }else {
        $(".butto").remove();
        $(".t_bg3").parent().remove();
        $(".t_bg2+ul li:eq(1)").remove();
        if (user == null) {
            $(".left_m a").attr("href","Login.html");
        }
        $($table).append("<tr>\n" +
            "                    <th>新闻编号</th>\n" +
            "                    <th>新闻标题</th>\n" +
            "                    <th>创建时间</th>\n" +
            "                    <th>操作</th>\n" +
            "                </tr>")
        for(var i in newsList){
            var $tr=$("<tr>" +
                "<td>"+newsList[i].id+"</td>" +
                "<td><a href='Member_News_Show.html?id="+newsList[i].id+"'>"+newsList[i].title+"</a></td>" +
                "<td>"+newsList[i].createTime+"</td>" +
                "<td><a href='Member_News_Show.html?id="+newsList[i].id+"'>查看</a></td>" +
                "</tr>")
            $($table).append($tr);
        }
    }

}
function newsList(pageNow){
    //获取信息
    $.ajax({
        url:"easybuy/newsList",
        dataType:"json",
        data:{
            pageNow:pageNow,
            pageSize:10
        },
        success:function (page) {
            pageMax=page.pageMax;
            // $("#yeshu").html(page.pageNow+"/"+page.pageMax);
            var newsList=page.list;
            showList(newsList);
            //翻页
            var $pages="";
            var black=parseInt(page.pageNow)-1<1?1:parseInt(page.pageNow)-1;
            $pages=$pages+"<a href='javascript:void(0)' class='p_pre' onclick='newsList("+black+")'>上一页</a>";
            if(page.pageNow>=5){
                $pages=$pages+"<a href='javascript:void(0)' onclick='newsList(1)'>1</a>...<a href='javascript:void(0)' onclick='newsList("+(parseInt(page.pageNow)-1)+")'>"+(parseInt(page.pageNow)-1)+"</a>";
            }else{
                for(i=1;i<page.pageNow;i++){
                    $pages=$pages+"<a href='javascript:void(0)' onclick='newsList("+i+")'>"+i+"</a>";
                }
            }
            $pages=$pages+"<a href='javascript:void(0)' class='cur'>"+page.pageNow+"</a>";
            if(page.pageMax-page.pageNow>=4){
                $pages=$pages+"<a href='javascript:void(0)' onclick='newsList("+(parseInt(page.pageNow)+1)+")'>"+(parseInt(page.pageNow)+1)+"</a>...<a href='javascript:void(0)' onclick='newsList("+page.pageMax+")'>"+page.pageMax+"</a>";
            }else{
                for(i=page.pageNow+1;i<=page.pageMax;i++){
                    $pages=$pages+"<a href='javascript:void(0)' onclick='newsList("+i+")'>"+i+"</a>";
                }
            }
            var next=parseInt(page.pageNow)+1>parseInt(page.pageMax)?parseInt(page.pageMax):parseInt(page.pageNow)+1;
            $pages=$pages+"<a href='javascript:void(0)' class='p_pre' onclick='newsList("+next+")'>下一页</a>";
            $(".pages").html($pages);
        }
    })
}
$(function () {
    newsList(pageNow);
})